/**
 * Created by Administrator on 2016/12/19.
 */
class FrontUIContainer extends ComponentEx {

    constructor() {
        super();
        this.funcIcons = [];
        this.initView();
    }

    replayFuncView_1:egret.DisplayObjectContainer;
    replayFuncView_2:egret.DisplayObjectContainer;
    replayFuncView_3:egret.DisplayObjectContainer;
    replayFuncView_4:egret.DisplayObjectContainer;

    funcIcons: egret.Bitmap[];
    currDir: number;
    //反向查找到功能对应的index
    indexs: any;
    hand: egret.Bitmap;

    childrenCreated() {
        super.childrenCreated();
        this.top = this.bottom = this.left = this.right = 0;
    }

    initView() {
        for (let i: number = 1; i <= 4; i++) {
            let pos = GSConfig.replayFuncPos[i];
            let view = new egret.DisplayObjectContainer;
            view.x = pos.x;
            view.y = pos.y;
            this.addChild(view);

            this["replayFuncView_" + i] = view;
        }

        this.hand = new egret.Bitmap(RES.getRes("RP_hand"));
        this.hand.scaleX = this.hand.scaleY = .5;
        this.hand.visible = false;
        this.addChild(this.hand);
    }

    //手标聚焦到对应的菜单选项上
    handFocus(func: number) {
        let pos = this.getFunIconPos(func);
        if (pos) {
            this.hand.visible = true;
            this.hand.x = pos.x;
            this.hand.y = pos.y;
        }
        this.indexs = {};
    }

    getFunIconPos(func: number) {
        let pos = GSConfig.replayFuncPos[this.currDir];
        let index = this.indexs[func];
        let returnPos: any = null;
        if (index > -1) {
            returnPos = {
                x: pos.x + index * pos.dx,
                y: pos.y + index * pos.dy
            }
        }
        return returnPos;
    }

    addReplayFuncs(dir: number, funcs: any[]) {
        this.currDir = dir;
        let pos = GSConfig.replayFuncPos[dir];
        let view = this["replayFuncView_" + dir];
        //记录重复的index
        let indexObj = {};
        let index = 0;

        this.indexs = {};

        for (let i: number = 0; i < funcs.length; i++) {
            let func: number = funcs[i];
            let resIndex = gameManager.dataManager.getAction2Res(func);
            //有相同的资源
            if (indexObj[resIndex] > -1) {
                this.indexs[func] = indexObj[resIndex];

            }
            else {
                let bitmap = this.createIcon(func);
                bitmap.x = pos.dx * index;
                bitmap.y = pos.dy * index;
                view.addChild(bitmap);
                this.indexs[func] = index;
                indexObj[resIndex] = index;

                index++;
            }

        }
    }

    createIcon(id: number) {
        let bitmap = null;
        if (this.funcIcons.length) {
            bitmap = this.funcIcons.shift();
        }
        else {
            bitmap = new egret.Bitmap;
        }

        bitmap.texture = RES.getRes('menu_' + gameManager.dataManager.getAction2Res(id));
        bitmap.anchorOffsetX = bitmap.width >> 1;
        bitmap.anchorOffsetY = bitmap.height >> 1;
        return bitmap;
    }

    returnIcon(icon: egret.Bitmap) {
        icon.texture = null;
        this.funcIcons.push(icon);
    }

    clear() {
        for (let i: number = 1; i <= 4; i++) {
            let view: egret.DisplayObjectContainer = this["replayFuncView_" + i];
            while (view.numChildren) {
                let icon: egret.Bitmap = <egret.Bitmap> view.getChildAt(0);
                view.removeChild(icon);
                this.returnIcon(icon);
            }
        }
        this.hand.visible = false;
    }
}